package org.sjlt.marc.workers;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.sjlt.marc.common.BigHelpDBConnection;
import org.sjlt.marc.common.Queries;
import org.sjlt.marc.resources.userData;

public class FindUser implements Runnable, Serializable 
{
	private static final long serialVersionUID = 838161834988951194L;
	private static final Logger logger = Logger.getLogger(FindUser.class);
	
	userData returnData;
	Vector<userData> userList = new Vector<userData>();
	
	public FindUser(String userid)
	{
		getUser(userid);
	}
	public FindUser()
	{
		getAllUsers();
	}

	public void getUser(String userid)
	{
		BigHelpDBConnection bhdb = new BigHelpDBConnection("sjlt_marc");
		try
		{
			Connection conn = bhdb.getConnection();
			try
			{
				PreparedStatement ps = conn.prepareStatement(Queries.SQL_FIND_USER);
				ps.setString(1, userid);
				try
				{
					ResultSet rs = ps.executeQuery();
					if(!rs.next())
					{
						returnData = null;
					}
					else
					{
						String UserId = rs.getString("UserId");
						String FName = rs.getString("FName");
						String MInitial = rs.getString("MInitial");
						String LName = rs.getString("LName");
						String Pref_Name = rs.getString("Pref_Name");
						Date DOB = rs.getDate("DOB");
						String Gender = rs.getString("Gender");
						String Address = rs.getString("Address");
						String City = rs.getString("City");
						String Zip = rs.getString("Zip");
						String Phone = rs.getString("Phone");
						Integer Carrier = rs.getInt("Carrier");
						String Email = rs.getString("Email");
						String Shirt = rs.getString("Shirt");
						String School = rs.getString("School");
						Integer Grade = Integer.parseInt(rs.getString("Grade"));
						String Highest_Sacrament = rs.getString("Highest_Sacrament");
						Integer Confirmation = Integer.parseInt(rs.getString("Confirmation"));
						String Access_Level = rs.getString("Access_Level");
						String InsuranceCo = rs.getString("InsuranceCo");
						String InsuranceNum = rs.getString("InsuranceNum");
						String Medications = rs.getString("Medications");
						String Allergies = rs.getString("Allergies");
						String MedicalIssues = rs.getString("MedicalIssues");
						Integer PermissionToTreat = Integer.parseInt(rs.getString("PermissionToTreat"));
						String EmgContact = rs.getString("EmgContact");
						String EmgRelation = rs.getString("EmgRelation");
						String EmgNumber = rs.getString("EmgNumber");
						String ReportsTo = rs.getString("ReportsTo");
						String Picture  = rs.getString("Picture");
						
						userData uData = new userData(UserId, FName, MInitial, LName, Pref_Name, DOB, Gender, Address, City, Zip, Phone, Carrier, Email, Shirt, 
								School, Grade, Highest_Sacrament, Confirmation, Access_Level, InsuranceCo, InsuranceNum, Medications, Allergies, 
								MedicalIssues, PermissionToTreat, EmgContact, EmgRelation, EmgNumber, ReportsTo, Picture);
						
						rs.close();
						
						returnData = uData;
					}
				}
				catch(SQLException se)
				{
					if(logger.isEnabledFor(Level.ERROR)){logger.error("SQL Exception.");}
					else
						se.printStackTrace();
				}
				catch(Exception e)
				{
					e.printStackTrace();
				}
				finally
				{
					ps.close();
				}
			}
			finally
			{
				bhdb.closeConnection();
			}
		}
		catch(SQLException e)
		{
			if(logger.isEnabledFor(Level.ERROR)){logger.error("SQL Exception while gathering org list.");}
			else{e.printStackTrace();}
		}
		finally
		{
			bhdb.closeConnection();
		}
	}
	public void getAllUsers()
	{
		BigHelpDBConnection bhdb = new BigHelpDBConnection("sjlt_marc");
		try
		{
			Connection conn = bhdb.getConnection();
			try
			{
				PreparedStatement ps = conn.prepareStatement(Queries.SQL_GATHER_ALL_USERS);
				try
				{
					ResultSet rs = ps.executeQuery();
					if(!rs.next())
					{
						returnData = null;
					}
					else
					{
						while(rs.next())
						{
							String UserId = rs.getString("UserId");
							String FName = rs.getString("FName");
							String MInitial = rs.getString("MInitial");
							String LName = rs.getString("LName");
							String Pref_Name = rs.getString("Pref_Name");
							Date DOB = rs.getDate("DOB");
							String Gender = rs.getString("Gender");
							String Address = rs.getString("Address");
							String City = rs.getString("City");
							String Zip = rs.getString("Zip");
							String Phone = rs.getString("Phone");
							Integer Carrier = rs.getInt("Carrier");
							String Email = rs.getString("Email");
							String Shirt = rs.getString("Shirt");
							String School = rs.getString("School");
							Integer Grade = Integer.parseInt(rs.getString("Grade"));
							String Highest_Sacrament = rs.getString("Highest_Sacrament");
							Integer Confirmation = Integer.parseInt(rs.getString("Confirmation"));
							String Access_Level = rs.getString("Access_Level");
							String InsuranceCo = rs.getString("InsuranceCo");
							String InsuranceNum = rs.getString("InsuranceNum");
							String Medications = rs.getString("Medications");
							String Allergies = rs.getString("Allergies");
							String MedicalIssues = rs.getString("MedicalIssues");
							Integer PermissionToTreat = Integer.parseInt(rs.getString("PermissionToTreat"));
							String EmgContact = rs.getString("EmgContact");
							String EmgRelation = rs.getString("EmgRelation");
							String EmgNumber = rs.getString("EmgNumber");
							String ReportsTo = rs.getString("ReportsTo");
							String Picture  = rs.getString("Picture");
							
							userData uData = new userData(UserId, FName, MInitial, LName, Pref_Name, DOB, Gender, Address, City, Zip, Phone, Carrier, Email, Shirt, 
									School, Grade, Highest_Sacrament, Confirmation, Access_Level, InsuranceCo, InsuranceNum, Medications, Allergies, 
									MedicalIssues, PermissionToTreat, EmgContact, EmgRelation, EmgNumber, ReportsTo, Picture);
							
							userList.add(uData);
							
						}
							rs.close();
					}
				}
				catch(SQLException se)
				{
					se.printStackTrace();
				}
				catch(Exception e)
				{
					e.printStackTrace();
				}
				finally
				{
					ps.close();
				}
			}
			finally
			{
				bhdb.closeConnection();
			}
		}
		catch(SQLException e)
		{
			if(logger.isEnabledFor(Level.ERROR)){logger.error("SQL Exception while gathering org list.");}
			else{e.printStackTrace();}
		}
		finally
		{
			bhdb.closeConnection();
		}
	}
	
	public userData getUserInfo()
	{
		return returnData;
	}
	
	public Vector<userData> getUserList()
	{
		return userList;
	}

	@Override
	public void run() {
		
		
	}
}
